//
//  FXStatusRetweetView.swift
//  微博(Swift)
//
//  Created by V on 16/1/3.
//  Copyright © 2016年 V. All rights reserved.
//

import UIKit
import SnapKit


// 配图视图里面最大的列数
let FXStatusPictureMaxCol = 3


class FXStatusRetweetView: UIView {
    // 记录当前控件的底部约束
    var bottomConstraint: Constraint?
    
    
    
    // 属性
    var statusViewModel: FXStatusViewModel? {
        didSet{
            contentLabel.text = statusViewModel?.retweetStatusText
            // 更新约束先卸载约束
            bottomConstraint?.uninstall()
            // 判断转发微博是否有配图，有就显示，没有就隐藏
            if let picurls = statusViewModel?.status?.retweeted_status?.pic_urls where picurls.count > 0 {
                // 有配图：1.显示配图视图 2.更新约束 3.设置要显示的数据
                statusPictureView.hidden = false
                self.snp_updateConstraints(closure: { (make) -> Void in
                    bottomConstraint = make.bottom.equalTo(statusPictureView).offset(FXStatusCellMargin).constraint
                })
                statusPictureView.picUrls = picurls
            }else{
                // 没有配图：1.隐藏视图配图 2.更新约束
                statusPictureView.hidden = true
                self.snp_updateConstraints(closure: { (make) -> Void in
                    bottomConstraint = make.bottom.equalTo(contentLabel).offset(FXStatusCellMargin).constraint
                })
            }
        }
    }
    
    
    

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    
    // MARK: - 懒加载
    // 内容
    private lazy var contentLabel: UILabel = {
        let label = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 15)
        label.numberOfLines = 0
        return label
    }()
    
    // 转发微博的图片
    private lazy var statusPictureView: FXStatusPictureView = FXStatusPictureView()
    
    
    
    
    
    private func setupUI(){
        backgroundColor = UIColor(white: 0.95, alpha: 1)
        // 添加视图
        addSubview(contentLabel)
        addSubview(statusPictureView)
        // 添加约束
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self).offset(FXStatusCellMargin)
            make.leading.equalTo(self).offset(FXStatusCellMargin)
            make.width.lessThanOrEqualTo(self.snp_width).offset(2 * -FXStatusCellMargin)
        }
        statusPictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLabel.snp_bottom).offset(FXStatusCellMargin)
            make.leading.equalTo(contentLabel)
            make.size.equalTo(CGSize(width: 100, height: 100))
        }
        self.snp_makeConstraints { (make) -> Void in
            bottomConstraint = make.bottom.equalTo(statusPictureView).offset(FXStatusCellMargin).constraint
        }
    }
}
